package pailaidai.hc.com.mysvgdemo.smalltools;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.SystemClock;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import pailaidai.hc.com.mysvgdemo.R;

/**
 * Created by Administrator on 2018/5/10.
 */

public class MyAppWidgetProvider extends AppWidgetProvider {
    public static final String TAG = "MyAppWidgetProvider";
    public static final String CLICK_ACTION = "com.ryg.chapter_5.action.CLICK";

    public MyAppWidgetProvider() {
        super();
    }

    @Override
    public void onReceive(final Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.d(TAG, "onReceive:action=" + intent.getAction());
        //这里判断是自己的action，做自己的事情，比如小部件被单击了要干什么，这里做一个动画效果
        if (intent.getAction().equals(CLICK_ACTION)) {
            Toast.makeText(context, "Clicked it", Toast.LENGTH_SHORT).show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Bitmap srcbBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.dialog_loading);
                    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                    for (int i = 0; i < 37; i++) {
                        float degree = (i * 10) / 360;
                        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_radar_layout);
                        remoteViews.setImageViewBitmap(R.id.radr_image, rotateBitmap(context, srcbBitmap, degree));
                        Intent intentClick = new Intent();
                        intentClick.setAction(CLICK_ACTION);
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);
                        remoteViews.setOnClickPendingIntent(R.id.radr_button, pendingIntent);
                        appWidgetManager.updateAppWidget(new ComponentName(context, MyAppWidgetProvider.class), remoteViews);
                        SystemClock.sleep(30);
                    }
                }
            }).start();
        }
    }


    /**
     * 每次左面小部件更新时都调用一次该方法
     * */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.i(TAG,"onUpdate");
        final int counter=appWidgetIds.length;
        Log.i(TAG,"counter="+counter);
        for(int i=0;i<counter;i++){
            int appWidgetId=appWidgetIds[i];
            onwidgetUpdate(context,appWidgetManager,appWidgetId);
        }
    }

    private void onwidgetUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
       Log.d(TAG,"appWidgetId="+appWidgetId);
        RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.widget);
       //桌面小部件 单击事件发送的Intent广播
        Intent intent=new Intent();
        intent.setAction(CLICK_ACTION);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,intent,0);
        remoteViews.setOnClickPendingIntent(R.id.radr_text,pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId,remoteViews);
    }


    private Bitmap rotateBitmap(Context context, Bitmap srcbBitmap, float degree) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.setRotate(degree);
        Bitmap bitmap = Bitmap.createBitmap(srcbBitmap, 0, 0, srcbBitmap.getWidth(), srcbBitmap.getHeight(), matrix, true);
        return bitmap;
    }


}
